package com.example.blog.modules.system.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.common.security.HasPermission;
import com.example.blog.modules.system.dto.FileUploadDTO;
import com.example.blog.modules.system.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "文件管理", description = "文件上传下载相关接口")
@RestController
@RequestMapping("/api/files")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @Operation(summary = "上传单个文件")
    @PostMapping("/upload")
    @HasPermission("file:upload")
    public ApiResponse<FileUploadDTO> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "directory", defaultValue = "common") String directory) {
        return ApiResponse.success(fileService.uploadFile(file, directory));
    }

    @Operation(summary = "批量上传文件")
    @PostMapping("/batch-upload")
    @HasPermission("file:upload")
    public ApiResponse<List<FileUploadDTO>> uploadFiles(
            @RequestParam("files") List<MultipartFile> files,
            @RequestParam(value = "directory", defaultValue = "common") String directory) {
        return ApiResponse.success(fileService.uploadFiles(files, directory));
    }

    @Operation(summary = "删除文件")
    @DeleteMapping
    @HasPermission("file:delete")
    public ApiResponse<Void> deleteFile(@RequestParam("fileUrl") String fileUrl) {
        fileService.deleteFile(fileUrl);
        return ApiResponse.success();
    }
} 